package cn.kingyang.yiyi.utils;


import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fafala.paylib.FaFaLaPay;
import com.fafala.paylib.callbacks.FFLPayCallback;
import com.fafala.paylib.models.OrderBean;

import java.util.Random;

import cn.kingyang.yiyi.R;
import cn.kingyang.yiyi.app.App;

/**
 * Created by Administrator on 2017/2/28.
 */

public class AppUtil {
    public static void initImage(Context context, View view, String url){
        Glide.with(context)
                .load(url)
                .into((ImageView)view);
    }
    public static int getRandom(int min,int max){
        Random random = new Random();

        return random.nextInt(max)%(max-min+1) + min;
    }
//    static PopupWindow popupWindow;
    public static void showPopupWindow(Context context, View view){
        showPopupWindow(context,view,false);
    }
    public static PopupWindow popupWindow = null;
    public static void showPopupWindow(final Context context, View view,boolean isback) {
//        Toast.makeText(context,App.level+"",Toast.LENGTH_LONG).show();
        // 一个自定义的布局，作为显示的内容

        View contentView = LayoutInflater.from(context).inflate(
                R.layout.pop_recharge, null);
        // 设置按钮的点击事件
        ImageView pic= (ImageView) contentView.findViewById(R.id.iv1);
        pic.setImageResource(App.popic.get(App.level));
        ImageView imageView = (ImageView) contentView.findViewById(R.id.close);
        TextView zhifu = (TextView) contentView.findViewById(R.id.zhifu);
        final RadioButton zhifubao= (RadioButton) contentView.findViewById(R.id.zhifubao);
        TextView jiage= (TextView) contentView.findViewById(R.id.jiage);
        if(isback)
            jiage.setText("恭喜你获得五折优惠"+38*(App.level+1)/2+"元!");
        else
            jiage.setText("限时特价仅需"+38*(App.level+1)+"元!");
        zhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                AppUtil.Pay((Activity) context,38*(App.level+1),"测试测试", PayManagerConfig.PAY_WECHAT);
                AppUtil.Pay((Activity) context,1, "升级为"+App.levels.get(App.level+1)+"会员", zhifubao.isChecked()?FaFaLaPay.TYPE_ALIPAY:FaFaLaPay.TYPE_WECHAT);
            }
        });
//        if(popupWindow==null) {
       popupWindow = new PopupWindow(contentView,
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            imageView.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            popupWindow.setTouchable(true);

            popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return false;

                }
            });
//        }
//        if (popupWindow.isShowing())
        popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
    }
   public static void Pay(final Activity activity, int price, String name,int payType){

//       PayManager.getInstance().payment(activity,price,Const.APP.PAXCHANNEL,cpParameter,payType,new IFusionPay(){
//
//           @Override
//           public void payFusionSuccess() {

//               if (popupWindow.isShowing())
//                   popupWindow.dismiss();
//           }
//
//           @Override
//           public void payFusionFail(int i) {
//               Toast.makeText(activity,"错误代码："+i,Toast.LENGTH_LONG).show();
//           }
//       });
       OrderBean orderBean = new OrderBean();
       orderBean.setMchTradeNo(System.currentTimeMillis()+""+AppUtil.getRandom(1000,9999));// 商户自己的订单号，必须全局唯一，32个字符内
       orderBean.setNotifyUrl("http://api.ppyingshi.com:88/api/order"); //异步回调地址，支付成功后发发啦后台通过该地址回调支付数据给商户后台
       orderBean.setTradeContent(name);
       orderBean.setTradeAttach("测试");// 回调时我们会原样把附加信息返回
       orderBean.setTradeMoney(price); //支付金额（单位：分）
       FaFaLaPay.getInstance().startPay(activity,payType, orderBean,new FFLPayCallback(){

           @Override
           public void onPayResuilt(int i, String s) {
               switch(i){
                    case 0:
                           if(AppUtil.popupWindow.isShowing())
                               AppUtil.popupWindow.dismiss();
                           App.level++;
                           SharedPreferencesUtils.setParam(activity,"level",App.level);
                           Toast.makeText(activity,"恭喜您成功开通"+App.levels.get(App.level),Toast.LENGTH_LONG).show();
                           break;
                    case 1:
                           Toast.makeText(activity,"支付失败",Toast.LENGTH_LONG).show();
                           break;
               }
//               Toast.makeText(activity,"返回码"+i,Toast.LENGTH_LONG).show();
           }
       });
   }
}
